﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Communergy.Common;
using Communergy.Repository.SQLite;


namespace Communergy.Repository
{
    public class SQLiteMeterRepository : IMeterRepository
    {
		#region Fields (1) 
        CommunergySQLiteEntities sqlite;
        

		#endregion Fields 

		#region Constructors (1) 

        public SQLiteMeterRepository(IUnitOfWork unitOfWork)
        {
            //will return null if not communergyEntities
            sqlite = unitOfWork as CommunergySQLiteEntities;
            if (sqlite == null)
                throw new ArgumentOutOfRangeException("unitOfWork");           
        }

       
		#endregion Constructors 

		#region Methods (2) 

		// Private Methods (2) 

        private Meter CommunergyMeterFromSqlLiteMeter(ElectricityMeter sqlLiteMeter)
        {
            if (sqlLiteMeter != null)
            {
                Meter communergyMeter = new Meter
                {
                    MeterId = (int)sqlLiteMeter.id,
                    MeterName = sqlLiteMeter.meterName,
                    MeterGuid = sqlLiteMeter.meterGuid
                };
                return communergyMeter;
            }
            else
            {
                return null;
            }

        }

        private ElectricityMeter SqlLiteMeterFromCommunergyMeter(Meter communergyMeter)
        {
            if (communergyMeter != null)
            {
                var sqlLiteMeter = new ElectricityMeter
                {
                    id = communergyMeter.MeterId,
                    meterGuid = communergyMeter.MeterGuid,
                    meterName = communergyMeter.MeterName
                };
                return sqlLiteMeter;
            }
            else
            {
                return null;
            }
        }

		#endregion Methods 

        #region IMeterRepository Members

        public Meter CreateNewMeter(Meter meterToCreate)
        {
            //using (var sqlite = new CommunergySQLiteEntities(connString))
            {
                var sqlLiteMeter = SqlLiteMeterFromCommunergyMeter(meterToCreate);
                
                sqlite.AddToElectricityMeter(sqlLiteMeter);
                sqlite.SaveChanges();
                Meter savedMeter = CommunergyMeterFromSqlLiteMeter(sqlLiteMeter);
                return savedMeter;

            }
        }

     

        public Meter RetrieveMeterDetails(string meterId)
        {
            var intLocalMeterId = Convert.ToInt32(meterId);
            //using (var sqlite = new CommunergySQLiteEntities(connString))
            {
                var getMeter = sqlite.ElectricityMeter.Where(m => m.id == intLocalMeterId).FirstOrDefault();
                return CommunergyMeterFromSqlLiteMeter(getMeter);
            }

        }



        public void SaveMeterDetails(Meter meterToSave)
        {
            //using (var sqlite = new CommunergySQLiteEntities(connString))
            {
                var sqlMeterToUpdate = sqlite.ElectricityMeter.Where(m => m.id == meterToSave.MeterId).FirstOrDefault();
                sqlMeterToUpdate.meterGuid = meterToSave.MeterGuid;
                sqlMeterToUpdate.meterName = meterToSave.MeterName;
                //sqlite.SaveChanges();
            }
        }

        public List<Meter> GetMetersForUser(string userId)
        {
            //using (var sqlite = new CommunergySQLiteEntities(connString))
            {
                var rawMeters = sqlite.ElectricityMeter.ToList();
                var meters = (from m in rawMeters
                              orderby m.meterName
                              select new Meter
                              {
                                  MeterId = (int)m.id,
                                  MeterGuid = m.meterGuid ,
                                  MeterName = m.meterName
                              }).ToList();               
                return meters;
            }
        }

        #endregion
    }
}
